package org.example.demo4.demos.controller;

import org.example.demo4.demos.controller.request.AddClassRequest;
import org.example.demo4.demos.controller.request.UpdateClassRequest;
import org.example.demo4.demos.controller.request.UpdateInfoRequest;
import org.example.demo4.demos.controller.request.UpdatePasswordRequest;
import org.example.demo4.demos.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/update-password")
    public ResponseEntity<String> updatePassword(@RequestBody UpdatePasswordRequest request) {
        try {
            adminService.updatePassword(request.getAdminId(), request.getNewPassword());
            return new ResponseEntity<>("Password updated successfully.", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Failed to update password: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/update-info")
    public ResponseEntity<String> updateInfo(@RequestBody UpdateInfoRequest request) {
        try {
            adminService.updateInfo(request.getAdminId(), request.getNewInfo());
            return new ResponseEntity<>("Information updated successfully.", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Failed to update information: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/addClass")
    public ResponseEntity<String> addClass(@RequestBody AddClassRequest request) {
        try {
            adminService.addClass(request.getClassName(), request.getGrade());
            return new ResponseEntity<>("Class added successfully.", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Failed to add class: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/updateClass")
    public ResponseEntity<String> updateClass(@RequestBody UpdateClassRequest request) {
        try {
            adminService.updateClass(request.getClassId(), request.getUpdatedClassName(), request.getUpdatedGrade());
            return new ResponseEntity<>("Class updated successfully.", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Failed to update class: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @DeleteMapping("/deleteClass/{classId}")
    public ResponseEntity<String> deleteClass(@PathVariable String classId) {
        try {
            adminService.deleteClass(classId);
            return new ResponseEntity<>("Class deleted successfully.", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Failed to delete class: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
